﻿using DataCollectService;
using Microsoft.Owin.Hosting;
using Syntec.Remote;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WebApp.Start<Startup>("http://127.0.0.1:10008/");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            testData td = new testData();
            td.sex = "男";
            td.listProp = new List<Prop>();
            td.listProp.Add(new Prop()
            {
                name = "属性1"
            });
            string rs = td.ToJSON();
            var dm = rs.FromJSON<dynamic>();
            string lp = dm["listProp"].ToString();
            List<dynamic> ld = lp.FromJSON<List<dynamic>>();
            string name = ld[0].name;


        }

        private void button3_Click(object sender, EventArgs e)
        {
            DeviceConfig protocal = new DeviceConfig();
            string result = @"{""msgResult"":[{""device_id"":""SK - C21"",""device_type"":""syntec"",""ts"":1740363155905,""values"":[{""name"":""cnc_ablpos"",""value"":[{""axis"":""X"",""value"":12.244},{""axis"":""Y"",""value"":-28.406},{""axis"":""Z"",""value"":-15.8}]},{""name"":""cnc_actspeed"",""value"":1000.0},{""name"":""cnc_alarm"",""value"":[]},{""name"":""cnc_alivetime"",""value"":9462},{""name"":""cnc_cuttime"",""value"":28322473},{""name"":""cnc_cycletime"",""value"":75},{""name"":""cnc_frate"",""value"":100.0},{""name"":""cnc_gcode"",""value"":""G00 / G18 / G90 / G22 / G95 / G71 / G40 / G43 / G50 / G97 / G64 / G69 / G26 / G13 / ""},{""name"":""cnc_ip"",""value"":""192.168.1.220""},{""name"":""cnc_mainproname"",""value"":""ALT844MFZ""},{""name"":""cnc_mecpos"",""value"":[{""axis"":""X"",""value"":-332.154},{""axis"":""Y"",""value"":-28.406},{""axis"":""Z"",""value"":-129.723}]},{""name"":""cnc_mode"",""value"":2},{""name"":""cnc_products"",""value"":2243},{""name"":""cnc_products_current"",""value"":19},{""name"":""cnc_products_require"",""value"":20},{""name"":""cnc_relpos"",""value"":[{""axis"":""X"",""value"":-332.226},{""axis"":""Y"",""value"":-28.406},{""axis"":""Z"",""value"":-129.723}]},{""name"":""cnc_respos"",""value"":[{""axis"":""X"",""value"":-2.238},{""axis"":""Y"",""value"":0.0},{""axis"":""Z"",""value"":0.0}]},{""name"":""cnc_runstatus"",""value"":2},{""name"":""cnc_runtime"",""value"":28311001},{""name"":""cnc_seq"",""value"":11},{""name"":""cnc_setspeed"",""value"":100.0},{""name"":""cnc_srate"",""value"":60.0},{""name"":""cnc_type"",""value"":""10.116.16.13""},{""name"":""device_state"",""value"":0}]}]}";
            protocal.VarJSONList = new List<DeviceJSONConfig>(){new DeviceJSONConfig()
            {
                EquipJSONConfigID = "05f17e9fbf664846a52c8b6d10212c1d",
                ParentID = "1a685aa739f649588720e7ee18c12d20",
                NodeName = "device_type",
                NodeType = "属性"
            }, new DeviceJSONConfig()
            {
                EquipJSONConfigID = "1a685aa739f649588720e7ee18c12d20",
                ParentID = "00000000000000000000000000000000",
                NodeName = "msgResult",
                NodeType = "数组"
            }, new DeviceJSONConfig()
            {
                EquipJSONConfigID = "433dd2a2f72d496291a5068e666b4bbf",
                ParentID = "445a0526ef8c4b99b75902a8cc22583e",
                NodeName = "name",
                NodeType = "参数名"
            },new DeviceJSONConfig()
            {
                EquipJSONConfigID = "445a0526ef8c4b99b75902a8cc22583e",
                ParentID = "1a685aa739f649588720e7ee18c12d20",
                NodeName = "values",
                NodeType = "数组"
            },new DeviceJSONConfig()
            {
                EquipJSONConfigID = "599a0748153f41dfbe465e70ff453f25",
                ParentID = "1a685aa739f649588720e7ee18c12d20",
                NodeName = "ts",
                NodeType = "属性"
            }, new DeviceJSONConfig()
            {
                EquipJSONConfigID = "a35976f8782445088f8b2fd4aed771fa",
                ParentID = "445a0526ef8c4b99b75902a8cc22583e",
                NodeName = "value",
                NodeType = "参数值"
            }, new DeviceJSONConfig()
            {
                EquipJSONConfigID = "ecb81b52d1d5494791dcd6e43f04dd8f",
                ParentID = "1a685aa739f649588720e7ee18c12d20",
                NodeName = "device_id",
                NodeType = "属性"
            } }
            ;

            GetJSONTreeData(protocal, new Guid().ToString("N"), result.FromJSON<dynamic>());
        }




        private void GetJSONTreeData(DeviceConfig deviceConfig, string parentID, dynamic msgResult)
        {
            //MQTT服务端订阅返回都是JSON格式数据
            var varJSONList = deviceConfig.VarJSONList;
            foreach (var jsonConfig in varJSONList)
            {
                //根节点
                if (jsonConfig.ParentID == parentID)
                {
                    if (jsonConfig.NodeType == "数组")
                    {
                        string nodeJsonStr = msgResult[jsonConfig.NodeName].ToString();
                        var nodeList = nodeJsonStr.FromJSON<List<dynamic>>();
                        foreach (var node in nodeList)
                        {
                            GetJSONTreeData(deviceConfig, jsonConfig.EquipJSONConfigID, node);
                        }
                    }
                    else if (jsonConfig.NodeType == "对象")
                    {
                        string nodeJsonStr = msgResult[jsonConfig.NodeName].ToString();
                        var nodeObj = nodeJsonStr.FromJSON<dynamic>();
                        GetJSONTreeData(deviceConfig, jsonConfig.EquipJSONConfigID, nodeObj);
                    }
                    else if (jsonConfig.NodeType == "属性")
                    {
                        string nodeJsonStr = msgResult[jsonConfig.NodeName].ToString();
                        //var nodeObj = nodeJsonStr.FromJSON<dynamic>();
                    }
                    else if (jsonConfig.NodeType == "参数值")
                    {
                        string nodeJsonStr = msgResult[jsonConfig.NodeName].ToString();
                        string keyName = jsonConfig.NodeName;
                        string keyValue = nodeJsonStr;
                        //如果配置了参数名
                        var paramNameJSON = varJSONList.Where(x => x.NodeType == "参数名").FirstOrDefault();
                        if (paramNameJSON != null)
                        {
                            if (msgResult[paramNameJSON.NodeName] != null)
                            {
                                keyName = msgResult[paramNameJSON.NodeName].ToString();
                            }
                        }
                        //dataInterfaceHelper.insertKV(deviceConfig.DeviceNo, DateTime.Now, keyName, nodeObj[jsonConfig.NodeName].ToString(), deviceConfig.SaveDataTime == "" ? 0 : int.Parse(deviceConfig.SaveDataTime));
                    }
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            FanucObj fanucObj = new FanucObj();
            fanucObj.ip = "192.168.2.213";
            fanucObj.ret = Focas1.cnc_allclibhndl3(fanucObj.ip, 8193, Convert.ToInt32(fanucObj.timeout), out fanucObj.h);
            MessageBox.Show(fanucObj.ret.ToString());


            Focas1.ODBPOS fos = new Focas1.ODBPOS();
            short num = Focas1.MAX_AXIS;
            short type = -1;
            short ret = Focas1.cnc_rdposition(fanucObj.h, type, ref num, fos);
            //重连晚点再做
            //if (ret == -16)
            //{
            //    ConnectFanuc(ref obj);
            //    ret = Focas1.cnc_rdposition(fanucObj.h, type, ref num, fos);

            //}
            MessageBox.Show("1:" + ret.ToString());
        }

        private void button5_Click(object sender, EventArgs e)
        {
            SQLiteHelper sqlLiteHelper = new SQLiteHelper("CJ");

        }

        private void button6_Click(object sender, EventArgs e)
        {
            SyntecRemoteCNC cnc = new SyntecRemoteCNC("192.168.1.210");
            int CurSeq = 0;
            string MainProg = "", CurProg = "", Mode = "", Status = "", Alarm = "", EMG = "";
            short result = cnc.READ_status(out MainProg, out CurProg, out CurSeq, out Mode, out Status, out Alarm, out EMG);
            MessageBox.Show(result.ToString());
        }






        public class testData
        {
            public List<Prop> listProp { get; set; }
            public string sex { get; set; }
        }

        public class Prop
        {
            public string name { get; set; }
        }
    }
}
